package com.endpoint.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.endpoint.book.entity.BookComment;
import com.endpoint.book.mapper.BookCommentMapper;
import com.endpoint.book.service.BookCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.endpoint.book.vo.MemberCommentVo;
import com.endpoint.common.constant.ResponseStatus;
import com.endpoint.common.utils.ResultBean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 小说评论表 服务实现类
 * </p>
 *
 * @author Jack_Li
 * @since 2024-07-03
 */
@Service
public class BookCommentServiceImpl extends ServiceImpl<BookCommentMapper, BookComment> implements BookCommentService {

    @Override
    public ResultBean saveComment(MemberCommentVo memberCommentVo) {
        //校验用户是否已发表评论
        Long count = baseMapper.selectCount(new QueryWrapper<BookComment>()
                .eq("create_user_id", memberCommentVo.getUserId())
                .eq("book_id", memberCommentVo.getBookId())).longValue();
        if(count.intValue()>0){
            //用户已经发表评论
            return ResultBean.fail(ResponseStatus.HAS_COMMENTS);
        }
        //用户未发表该书评论 保存评论
        BookComment bookComment = new BookComment();
        bookComment.setBookId(memberCommentVo.getBookId());
        bookComment.setCreateUserId(memberCommentVo.getUserId());
        bookComment.setCreateTime(new Date());
        bookComment.setCommentContent(memberCommentVo.getCommentContent());
        baseMapper.insert(bookComment);
        return ResultBean.ok();
    }

}
